import eacharts from 'echarts'
import { ref, markRaw } from 'vue'
// 先导出一个函数,两个参数，实例与配置项
export function useChart(chartRef, setChart) {
    let chartInstance = ref(null)
    // 初始化eachart表
    const initChart = () => {
        if (chartRef.value) {
            // 
            chartInstance.value = markRaw(eacharts.init(chartRef.value))
            //
            let options = setChart()
            chartInstance.value.setOption(options)
        }
    }
    const resizeChart = () => {
        chartInstance.value?.resize()
    }
    onMounted(() => {
        initChart()
        window.addEventListener('resize', resizeChart)
    })
    onBeforeUnmount(()=>{
        window.removeEventListener('resize', resizeChart)
        if (chartInstance.value) {
            chartInstance.value.dispose()
        }
    })
}